package com.eshop.dao.Impl;

import com.eshop.dao.AddressDao;
import com.eshop.model.Address;

import java.util.List;

public class AddressDaoImpl extends BaseDao implements AddressDao {
    @Override
    public List<Address> queryAddressByUserId(int userId) {
        String sql = "select * from t_address where user_id = ?";
        return queryForList(Address.class,sql,userId);
    }

    @Override
    public Address queryAddressByAddressId(int addressId) {
        String sql = "select * from t_address where addr_id = ?";
        return queryForOne(Address.class,sql,addressId);
    }

    @Override
    public void addAddress(Address address) {
        String sql = "INSERT INTO `eshop0`.`t_address`(`user_id`, `addr_province`, `addr_city`, `addr_area`, `addr_content`, `addr_receiver`, `addr_tel`, `addr_isdefault`) VALUES (?,?,?,?,?,?,?,?)";
        update(sql,address.getUser_id(),address.getAddr_province(),address.getAddr_city(),address.getAddr_area(),address.getAddr_content(),address.getAddr_receiver(),address.getAddr_tel(),address.getAddr_isdefault());
    }

    @Override
    public void deleteAddressByAddressId(int addressID) {
        String sql = "delete from t_address where addr_id = ?";
        update(sql,addressID);
    }

    @Override
    public void updateAddress(Address address) {
        String sql ="UPDATE `eshop0`.`t_address` SET `addr_province` = ?, `addr_city` = ?, `addr_area` = ?, `addr_content` = ?, `addr_receiver` = ?, `addr_tel` = ?, `addr_isdefault` = ? WHERE `addr_id` = ?";
        update(sql,address.getAddr_province(),address.getAddr_city(),address.getAddr_area(),address.getAddr_content(),address.getAddr_receiver(),address.getAddr_tel(),address.getAddr_isdefault(),address.getAddr_id());
    }

    @Override
    public Address queryAddressByUserIdAndDefault(int userID, int isDefault) {
        String sql = "select * from t_address where user_id=? and addr_isdefault=?";
        return queryForOne(Address.class,sql,userID,isDefault);
    }
}
